

jQuery(document).ready(function () {

    jQuery('a').tooltip();

    jQuery('a.user-delete').click(function () {
        var res = confirm("Do you really want to delete this user?");
        if (!res) {
            return false;
        }
    });

    /**
     * Log In form validation script
     ********************************************************************************/

    jQuery('.login-form #bodyContent_txtLoginId').blur(function () {
        var parentObj = jQuery(this).parent().parent();
        if (jQuery.trim(jQuery(this).val()) == '') {
            jQuery(parentObj).addClass('error');
            jQuery(parentObj).find('.help-inline').text('Login Id is required');
        } else {
            jQuery(parentObj).removeClass('error');
            jQuery(parentObj).find('.help-inline').text('');
        }
    });

    jQuery('.login-form #bodyContent_txtPassword').blur(function () {
        var parentObj = jQuery(this).parent().parent();
        if (jQuery.trim(jQuery(this).val()) == '') {
            jQuery(parentObj).addClass('error');
            jQuery(parentObj).find('.help-inline').text('Password is required');
        } else {
            jQuery(parentObj).removeClass('error');
            jQuery(parentObj).find('.help-inline').text('');
        }
    });

    jQuery('.login-form #bodyContent_LoginButton').click(function () {

        var loginId = jQuery.trim(jQuery('.login-form #bodyContent_txtLoginId').val());
        var password = jQuery.trim(jQuery('.login-form #bodyContent_txtPassword').val());
        var error = 0;

        /* password */
        if (password == '') {
            var passwordParentObj = jQuery('.login-form #bodyContent_txtPassword').parent().parent();
            jQuery(passwordParentObj).addClass('error');
            jQuery(passwordParentObj).find('.help-inline').text('Password is required');
            jQuery('.login-form #bodyContent_txtPassword').focus();
            error = 1;
        }

        /* login id */
        if (loginId == '') {
            var loginParentObj = jQuery('.login-form #bodyContent_txtLoginId').parent().parent();
            jQuery(loginParentObj).addClass('error');
            jQuery(loginParentObj).find('.help-inline').text('Login Id is required');
            jQuery('.login-form #bodyContent_txtLoginId').focus();
            error = 1;
        }

        if (error == 1) {
            return false;
        }

    });


    /**
     * Forget Password form validation script
     ********************************************************************************/
    jQuery('#ForgetPassword #bodyContent_txtEmail').blur(function () {
        var error = 0;
        var parentObj = jQuery(this).parent().parent();

        if (jQuery.trim(jQuery(this).val()) == '') {
            jQuery(parentObj).addClass('error');
            jQuery(parentObj).find('.help-block').text('Please enter email address');
            error = 1;
        } else if (!isValidEmailAddress(jQuery(this).val())) {
            jQuery(parentObj).addClass('error');
            jQuery(parentObj).find('.help-block').text('Invalid email address entered');
            error = 1;
        }

        if (!error) {
            jQuery(parentObj).removeClass('error');
            jQuery(parentObj).find('.help-block').text('');
        }
    });

    jQuery('#ForgetPassword #bodyContent_ResetPasswordButton').click(function () {
        var error = 0;
        var parentObj = jQuery('#ForgetPassword #bodyContent_txtEmail').parent().parent();

        if (jQuery.trim(jQuery('#ForgetPassword #bodyContent_txtEmail').val()) == '') {
            jQuery(parentObj).addClass('error');
            jQuery(parentObj).find('.help-block').text('Please enter email address');
            error = 1;
        } else if (!isValidEmailAddress(jQuery('#ForgetPassword #bodyContent_txtEmail').val())) {
            jQuery(parentObj).addClass('error');
            jQuery(parentObj).find('.help-block').text('Invalid email address entered');
            error = 1;
        }

        if (error == 0) {
            jQuery(parentObj).removeClass('error');
            jQuery(parentObj).find('.help-block').text('');
        } else {
            jQuery('#ForgetPassword #bodyContent_txtEmail').focus();
            return false;
        }
    });


    /**
     * Registered User form validation script
     ********************************************************************************/

    jQuery('#UserRegistrationForm #bodyContent_txtLoginId').blur(function () {
        var parentObj = jQuery(this).parent().parent();
        if (jQuery.trim(jQuery(this).val()) == '') {
            jQuery(parentObj).addClass('error');
            jQuery(parentObj).find('.help-inline').text('Login Id is required');
        } else {
            jQuery(parentObj).removeClass('error');
            jQuery(parentObj).find('.help-inline').text('');
        }
    });

    jQuery('#UserRegistrationForm #bodyContent_txtPassword').blur(function () {
        var parentObj = jQuery(this).parent().parent();
        if (jQuery.trim(jQuery('#UserRegistrationForm bodyContent_IsEditMode').val()) == "0") {
            if (jQuery.trim(jQuery(this).val()) == '') {
                jQuery(parentObj).addClass('error');
                jQuery(parentObj).find('.help-inline').text('Password is required');
            } else {
                jQuery(parentObj).removeClass('error');
                jQuery(parentObj).find('.help-inline').text('');
            }
        }
    });

    jQuery('#UserRegistrationForm #bodyContent_txtConfirmPassword').blur(function () {
        var parentObj = jQuery(this).parent().parent();
        if (jQuery.trim(jQuery('#UserRegistrationForm bodyContent_IsEditMode').val()) == "0") {
            if (jQuery.trim(jQuery(this).val()) == '') {
                jQuery(parentObj).addClass('error');
                jQuery(parentObj).find('.help-inline').text('Confirm Password is required');
            } else {
                jQuery(parentObj).removeClass('error');
                jQuery(parentObj).find('.help-inline').text('');
            }
        }
    });

    jQuery('#UserRegistrationForm #bodyContent_txtFirstName').blur(function () {
        var parentObj = jQuery(this).parent().parent();
        if (jQuery.trim(jQuery(this).val()) == '') {
            jQuery(parentObj).addClass('error');
            jQuery(parentObj).find('.help-inline').text('First Name is required');
        } else {
            jQuery(parentObj).removeClass('error');
            jQuery(parentObj).find('.help-inline').text('');
        }
    });

    jQuery('#UserRegistrationForm #bodyContent_txtLastName').blur(function () {
        var parentObj = jQuery(this).parent().parent();
        if (jQuery.trim(jQuery(this).val()) == '') {
            jQuery(parentObj).addClass('error');
            jQuery(parentObj).find('.help-inline').text('Last Name is required');
        } else {
            jQuery(parentObj).removeClass('error');
            jQuery(parentObj).find('.help-inline').text('');
        }
    });

    jQuery('#UserRegistrationForm #bodyContent_txtEmail').blur(function () {
        var parentObj = jQuery(this).parent().parent();
        if (jQuery.trim(jQuery(this).val()) == '') {
            jQuery(parentObj).addClass('error');
            jQuery(parentObj).find('.help-inline').text('Email Address is required');
        } else if (!isValidEmailAddress(jQuery.trim(jQuery(this).val()))) {
            jQuery(parentObj).addClass('error');
            jQuery(parentObj).find('.help-inline').text('Invalid Email Address entered');
        } else {
            jQuery(parentObj).removeClass('error');
            jQuery(parentObj).find('.help-inline').text('');
        }
    });


    jQuery('#UserRegistrationForm #bodyContent_RegisterUserButton').click(function () {

        var error = 0;
        var loginId = jQuery.trim(jQuery('#UserRegistrationForm #bodyContent_txtLoginId').val());
        var password = jQuery.trim(jQuery('#UserRegistrationForm #bodyContent_txtPassword').val());
        var confirmPassword = jQuery.trim(jQuery('#UserRegistrationForm #bodyContent_txtConfirmPassword').val());
        var firstName = jQuery.trim(jQuery('#UserRegistrationForm #bodyContent_txtFirstName').val());
        var lastName = jQuery.trim(jQuery('#UserRegistrationForm #bodyContent_txtLastName').val());
        var emailAddress = jQuery.trim(jQuery('#UserRegistrationForm #bodyContent_txtEmail').val());

        if (loginId == '') {
            var loginParent = jQuery('#UserRegistrationForm #bodyContent_txtLoginId').parent().parent();
            jQuery(loginParent).addClass('error');
            jQuery(loginParent).find('.help-inline').text('Login Id is required');
            error = 1;
        }

        if (password == '') {
            var passwordParent = jQuery('#UserRegistrationForm #bodyContent_txtPassword').parent().parent();
            jQuery(passwordParent).addClass('error');
            jQuery(passwordParent).find('.help-inline').text('Password is required');
            error = 1;
        }

        if (confirmPassword == '') {
            var confirmPasswordParent = jQuery('#UserRegistrationForm #bodyContent_txtConfirmPassword').parent().parent();
            jQuery(confirmPasswordParent).addClass('error');
            jQuery(confirmPasswordParent).find('.help-inline').text('Confirm Password is required');
            error = 1;
        } else if (password != confirmPassword) {
            var confirmPasswordParent = jQuery('#UserRegistrationForm #bodyContent_txtConfirmPassword').parent().parent();
            jQuery(confirmPasswordParent).addClass('error');
            jQuery(confirmPasswordParent).find('.help-inline').text('Confirm Password does not match with Password');
            error = 1;
        }

        if (firstName == '') {
            var firstNameParent = jQuery('#UserRegistrationForm #bodyContent_txtFirstName').parent().parent();
            jQuery(firstNameParent).addClass('error');
            jQuery(firstNameParent).find('.help-inline').text('First Name is required');
            error = 1;
        }

        if (lastName == '') {
            var lastNameParent = jQuery('#UserRegistrationForm #bodyContent_txtLastName').parent().parent();
            jQuery(lastNameParent).addClass('error');
            jQuery(lastNameParent).find('.help-inline').text('Last Name is required');
            error = 1;
        }

        if (emailAddress == '') {
            var emailAddressParent = jQuery('#UserRegistrationForm #bodyContent_txtEmail').parent().parent();
            jQuery(emailAddressParent).addClass('error');
            jQuery(emailAddressParent).find('.help-inline').text('Email Address enter email address');
            error = 1;
        } else if (!isValidEmailAddress(emailAddress)) {
            var emailAddressParent = jQuery('#UserRegistrationForm #bodyContent_txtEmail').parent().parent();
            jQuery(emailAddressParent).addClass('error');
            jQuery(emailAddressParent).find('.help-inline').text('Invalid Email Address entered');
            error = 1;
        }

        if (error == 1) {
            return false;
        }

    });


    jQuery('#UserRegistrationForm #bodyContent_UpdateUserButton').click(function () {

        var error = 0;
        var loginId = jQuery.trim(jQuery('#UserRegistrationForm #bodyContent_txtLoginId').val());
        var password = jQuery.trim(jQuery('#UserRegistrationForm #bodyContent_txtPassword').val());
        var confirmPassword = jQuery.trim(jQuery('#UserRegistrationForm #bodyContent_txtConfirmPassword').val());
        var firstName = jQuery.trim(jQuery('#UserRegistrationForm #bodyContent_txtFirstName').val());
        var lastName = jQuery.trim(jQuery('#UserRegistrationForm #bodyContent_txtLastName').val());
        var emailAddress = jQuery.trim(jQuery('#UserRegistrationForm #bodyContent_txtEmail').val());

        if (password != confirmPassword) {
            var confirmPasswordParent = jQuery('#UserRegistrationForm #bodyContent_txtConfirmPassword').parent().parent();
            jQuery(confirmPasswordParent).addClass('error');
            jQuery(confirmPasswordParent).find('.help-inline').text('Confirm Password does not match with Password');
            error = 1;
        }

        if (firstName == '') {
            var firstNameParent = jQuery('#UserRegistrationForm #bodyContent_txtFirstName').parent().parent();
            jQuery(firstNameParent).addClass('error');
            jQuery(firstNameParent).find('.help-inline').text('First Name is required');
            error = 1;
        }

        if (lastName == '') {
            var lastNameParent = jQuery('#UserRegistrationForm #bodyContent_txtLastName').parent().parent();
            jQuery(lastNameParent).addClass('error');
            jQuery(lastNameParent).find('.help-inline').text('Last Name is required');
            error = 1;
        }

        if (emailAddress == '') {
            var emailAddressParent = jQuery('#UserRegistrationForm #bodyContent_txtEmail').parent().parent();
            jQuery(emailAddressParent).addClass('error');
            jQuery(emailAddressParent).find('.help-inline').text('Email Address enter email address');
            error = 1;
        } else if (!isValidEmailAddress(emailAddress)) {
            var emailAddressParent = jQuery('#UserRegistrationForm #bodyContent_txtEmail').parent().parent();
            jQuery(emailAddressParent).addClass('error');
            jQuery(emailAddressParent).find('.help-inline').text('Invalid Email Address entered');
            error = 1;
        }

        if (error == 1) {
            return false;
        }

    });



    /**
     * Reset Password form validation script
     ********************************************************************************/
    jQuery('#ResetPasswordForm #bodyContent_txtNewPassword').blur(function () {
        var error = 0;
        var parentObj = jQuery(this).parent().parent();

        if (jQuery.trim(jQuery(this).val()) == '') {
            jQuery(parentObj).addClass('error');
            jQuery(parentObj).find('.help-inline').text('Please enter New Password');
        } else {
            jQuery(parentObj).removeClass('error');
            jQuery(parentObj).find('.help-inline').text('');
        }
    });

    jQuery('#ResetPasswordForm #bodyContent_txtConfirmNewPassword').blur(function () {
        var error = 0;
        var parentObj = jQuery(this).parent().parent();

        if (jQuery.trim(jQuery(this).val()) == '') {
            jQuery(parentObj).addClass('error');
            jQuery(parentObj).find('.help-inline').text('Please enter Confirm New Password');
            error = 1;
        } else if (jQuery.trim(jQuery(this).val()) != jQuery.trim(jQuery('#ResetPasswordForm #bodyContent_txtNewPassword').val())) {
            jQuery(parentObj).addClass('error');
            jQuery(parentObj).find('.help-inline').text('New Password and Confirm New Password does not match');
            jQuery('#ResetPasswordForm #bodyContent_txtNewPassword').val('');
            jQuery(this).val('');
            jQuery('#ResetPasswordForm #bodyContent_txtNewPassword').focus();
            error = 1;
        }

        if (!error) {
            jQuery(parentObj).removeClass('error');
            jQuery(parentObj).find('.help-inline').text('');
        }
    });

    jQuery('#ResetPasswordForm #bodyContent_ResetPasswordButton').click(function () {
        var error = 0;

        if (jQuery.trim(jQuery('#ResetPasswordForm #bodyContent_txtNewPassword').val()) == '') {
            var parentObj = jQuery('#ResetPasswordForm #bodyContent_txtNewPassword').parent().parent();
            jQuery(parentObj).addClass('error');
            jQuery(parentObj).find('.help-inline').text('Please enter New Password');
            error = 1;
        }

        if (jQuery.trim(jQuery('#ResetPasswordForm #bodyContent_txtConfirmNewPassword').val()) == '') {
            var parentObj = jQuery('#ResetPasswordForm #bodyContent_txtConfirmNewPassword').parent().parent();
            jQuery(parentObj).addClass('error');
            jQuery(parentObj).find('.help-inline').text('Please enter Confirm New Password');
            error = 1;
        } else if (jQuery.trim(jQuery('#ResetPasswordForm #bodyContent_txtNewPassword').val()) != jQuery.trim(jQuery('#ResetPasswordForm #bodyContent_txtConfirmNewPassword').val())) {
            var parentObj = jQuery('#ResetPasswordForm #bodyContent_txtConfirmNewPassword').parent().parent();
            jQuery(parentObj).addClass('error');
            jQuery(parentObj).find('.help-inline').text('New Password and Confirm New Password does not match');
            jQuery('#ResetPasswordForm #bodyContent_txtNewPassword').val('');
            jQuery('#ResetPasswordForm #bodyContent_txtConfirmNewPassword').val('');
            jQuery('#ResetPasswordForm #bodyContent_txtNewPassword').focus();
            error = 1;
        }

        if (error == 1) {
            return false;
        }

    });


});

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

